package mobile.game.common.config;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;

import mobile.game.common.config.json.JInfo;
import mobile.game.common.config.json.JNode;
import mobile.game.common.config.json.JPort;
import mobile.game.common.config.json.JRoot;
import mobile.game.common.config.json.JService;
import mobile.game.core.support.Utils;
import mobile.game.core.support.log.LogCore;

/**
 * @author Administrator
 *
 */
public class JConfig {

	/**
	 * 根节点
	 */
	private static JRoot ROOT;

	/**
	 * 当前节点名称
	 */
	private static String meNodeName = "";

	
	/**
	 * JService与JInfo的对照表
	 */
	private static Map<String, JInfo> mapJInfo = new HashMap<String, JInfo>();

	/**
	 * 获取Root跟节点
	 * 
	 * @return
	 */
	public static JRoot getROOT() {
		return ROOT;
	}

	public static void SetMeNodeName(String nodeName) {
		meNodeName = nodeName;
	}

	public static JNode getMe() {
		return ROOT.getNodeByName(meNodeName);
	}
	
	/**
	 * 初始化对照数据
	 */
	private static boolean init() {
		for (int i = 0; i < ROOT.getNodes().size(); ++i) {
			JNode jNode = ROOT.getNodes().get(i);

			for (int j = 0; j < jNode.getPorts().size(); ++j) {
				JPort jPort = jNode.getPorts().get(j);
				
				for (int k = 0; k < jPort.getServices().size(); ++k) {
					JService jService = jPort.getServices().get(k);
					
					JInfo jInfo = new JInfo(jNode, jPort, jService);
					if (null != mapJInfo.get(jInfo.getServiceName()) ) {
						LogCore.core.error("ServiceName重名:{}", jInfo.getServiceName());
						return false;
					}
					
					mapJInfo.put(jService.getName(), jInfo);
				}
			}
		}
		
		for (Map.Entry<String, JInfo> entry : mapJInfo.entrySet()) {
			System.out.println("ServName:" + entry.getKey() + ",node:" + entry.getValue().getNodeName() + ",port:" + entry.getValue().getPortName());
		}
		
		return true;
	}

	/**
	 * 载入Json配置文件
	 * 
	 * @param jsonFileName 配置文件名称，无需路径（查找）
	 *            "serverConfig.json"
	 * @return
	 */
	public static boolean load(String jsonFileName) {
		String jsonString = "";
		try {
			jsonString = Utils.readFileToString(jsonFileName, "UTF-8");
			ROOT = JSONObject.parseObject(jsonString, JRoot.class);
			
			return init();
		} catch (IOException e) {
			LogCore.config.error("", e);
		}

		return false;
	}

	/**
	 * 通过ServName查找JInfo
	 * @param ServName
	 * @return
	 */
	public static JInfo getJInfoByServName(String servName) {
		JInfo jInfo = mapJInfo.get(servName);
		return jInfo;
	}
	
	/**
	 * 通过Service的Type类型获取JInfo
	 * @param servType
	 * @return
	 */
	public static List<JInfo> getJInfoByServType(String servType) {
		List<JInfo> listInfo = new ArrayList<JInfo>();
		for (Map.Entry<String, JInfo> entry : mapJInfo.entrySet()) {
			if (entry.getValue().getjService().getType().equals(servType)) {
				listInfo.add(entry.getValue());
			}
		}
		
		return listInfo;
	}
	
	private static void Test() {
		for (int i = 0; i < ROOT.getNodes().size(); ++i) {
			JNode jNode = ROOT.getNodes().get(i);
			System.out.println("nodeName:" + jNode.getName());
			System.out.println("nodeAddr:" + jNode.getAddr());

			for (int j = 0; j < jNode.getPorts().size(); ++j) {
				JPort jPort = jNode.getPorts().get(j);

				System.out.println("portType:" + jPort.getType());
				System.out.println("portName:" + jPort.getName());

				for (int k = 0; k < jPort.getServices().size(); ++k) {
					JService jService = jPort.getServices().get(k);
					
					JInfo jInfo = new JInfo(jNode, jPort, jService);
					mapJInfo.put(jService.getName(), jInfo);

					System.out.println("serviceType:" + jService.getType());
					System.out.println("serviceName:" + jService.getName());
				}
			}
		}
		
		JNode nodeRpcDemo = ROOT.getNodeByName("ProtBufDemo");
		if (nodeRpcDemo != null) {
			System.out.println(nodeRpcDemo.getName());
		}
		
	}

	public static void main(String args[]) {
		
		System.setProperty("logFileName", "jConfig");
		
		JConfig.load("AllRunInOneNode.json");

		Test();

		List<JInfo> list = JConfig.getJInfoByServType("ConnService");
		for (JInfo jInfo : list) {
			System.out.println(jInfo);
		}
	}
}
